package cn.maszy.action.projectmanagement;


import java.io.UnsupportedEncodingException;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;

import cn.maszy.common.BaseAction;
import cn.maszy.common.Pager;
import cn.maszy.common.SessionFactory;
import cn.maszy.model.projectmanagement.B_T_Flow_ProjectManagement;
import cn.maszy.model.projectmanagement.B_T_Ufp_TaskManager;
import cn.maszy.model.projectmanagement.C_T_Sys_File;
import cn.maszy.service.projectmanagement.B_T_Flow_ProjectManagementService;
import cn.maszy.service.projectmanagement.B_T_Ufp_TaskManagerService;
import cn.maszy.service.projectmanagement.C_T_Sys_FileService;

public class TaskManageAction extends BaseAction<B_T_Ufp_TaskManager>{
	/**
	 * 版本信息
	 */
	private static final long serialVersionUID = 1L;
	@Autowired
	private B_T_Ufp_TaskManagerService b_T_Ufp_TaskManagerService;
	@Autowired
	private B_T_Flow_ProjectManagementService b_T_Flow_ProjectManagementService;
	@Autowired
	private C_T_Sys_FileService c_T_Sys_FileService;
	private C_T_Sys_File c_T_Sys_File;
	private B_T_Ufp_TaskManager b_T_Ufp_TaskManager;
	private B_T_Flow_ProjectManagement b_T_Flow_ProjectManagement;
	private Pager<B_T_Ufp_TaskManager> Pagers;
	private String checkIds;
	//项目ID
	private String projectUuid;
	//项目名称
	private String projectName;
	//逻辑判断
	private String flag;
	private String tab;
	//本次用时
	private double taskActualTimeSpent;
	//完成进度
	private String taskSpeed;
	//备注
	private String remark;
	//附件列表
	private Pager<C_T_Sys_File> pagerFile;
	/*********************class set/get****************************/
	public B_T_Ufp_TaskManager getB_T_Ufp_TaskManager() {
		return b_T_Ufp_TaskManager;
	}
	public void setB_T_Ufp_TaskManager(B_T_Ufp_TaskManager b_T_Ufp_TaskManager) {
		this.b_T_Ufp_TaskManager = b_T_Ufp_TaskManager;
	}
	public String getCheckIds() {
		return checkIds;
	}
	public void setCheckIds(String checkIds) {
		this.checkIds = checkIds;
	}
	public String getProjectUuid() {
		return projectUuid;
	}
	public void setProjectUuid(String projectUuid) {
		this.projectUuid = projectUuid;
	}
	public String getProjectName() {
		return projectName;
	}
	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}
	public String getFlag() {
		return flag;
	}
	public void setFlag(String flag) {
		this.flag = flag;
	}
	public String getTaskSpeed() {
		return taskSpeed;
	}
	public void setTaskSpeed(String taskSpeed) {
		this.taskSpeed = taskSpeed;
	}
	public double getTaskActualTimeSpent() {
		return taskActualTimeSpent;
	}
	public void setTaskActualTimeSpent(double taskActualTimeSpent) {
		this.taskActualTimeSpent = taskActualTimeSpent;
	}
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
	public Pager<B_T_Ufp_TaskManager> getPagers() {
		return Pagers;
	}
	public void setPagers(Pager<B_T_Ufp_TaskManager> pagers) {
		Pagers = pagers;
	}
	public B_T_Flow_ProjectManagement getB_T_Flow_ProjectManagement() {
		return b_T_Flow_ProjectManagement;
	}
	public void setB_T_Flow_ProjectManagement(
			B_T_Flow_ProjectManagement b_T_Flow_ProjectManagement) {
		this.b_T_Flow_ProjectManagement = b_T_Flow_ProjectManagement;
	}
	public Pager<C_T_Sys_File> getPagerFile() {
		return pagerFile;
	}
	public void setPagerFile(Pager<C_T_Sys_File> pagerFile) {
		this.pagerFile = pagerFile;
	}
	public C_T_Sys_File getC_T_Sys_File() {
		return c_T_Sys_File;
	}
	public void setC_T_Sys_File(C_T_Sys_File c_T_Sys_File) {
		this.c_T_Sys_File = c_T_Sys_File;
	}
	public String getTab() {
		return tab;
	}
	public void setTab(String tab) {
		this.tab = tab;
	}
	/*******************STRUTS ACTION METHOD*************************/
	/**
	 * 插入
	 */
	public String taskManageUpdate()
	{
		int i=b_T_Ufp_TaskManagerService.update(b_T_Ufp_TaskManager,flag,0.0,"","");
		if(i >0 )
		return ajaxForwardSuccess(getText("msg.operation.success"), "ProjectmanagementList","ProjectmanagementList", "forward", "projectmanagement/ProjectmanagementList?b_T_Flow_ProjectManagement.status=0");
		return ajaxForwardError(getText("msg.operation.failure"));
	}
	/**
	 * 项目查看信息任务编辑
	 */
	public String taskManageEditUpdate()
	{
		int i=b_T_Ufp_TaskManagerService.update(b_T_Ufp_TaskManager,flag,0.0,"","");
		if(b_T_Ufp_TaskManager.getTaskid() != null || !"".equals(b_T_Ufp_TaskManager.getTaskid().trim())){
			b_T_Ufp_TaskManager = b_T_Ufp_TaskManagerService.selectOne(b_T_Ufp_TaskManager.getTaskid());
		}
		if(i >0 )
		return ajaxForwardSuccess(getText("msg.operation.success"), "ProjectmanagementReade","relxz", "closeCurrent", "projectmanagement/ProjectmanagementReade?b_T_Flow_ProjectManagement.projectUuid="+b_T_Ufp_TaskManager.getProjectUuid()+"&moduleName=project&b_T_Ufp_TaskManager.taskSpeed=0");
		return ajaxForwardError(getText("msg.operation.failure"));
	}
	/**
	 * 插入
	 */
	/**
	 * @return
	 */
	public String taskManageUpdates()
	{
		int i = 0;
		if("2".equals(tab)){ //报工
			b_T_Ufp_TaskManager = b_T_Ufp_TaskManagerService.selectOne(b_T_Ufp_TaskManager.getTaskid());
			//old进度
			String taskActualSpent = b_T_Ufp_TaskManager.getTaskSpeed();
			if(b_T_Ufp_TaskManager!= null){
				if(b_T_Ufp_TaskManager.getTaskActualTimeSpent() == null){
					b_T_Ufp_TaskManager.setTaskActualTimeSpent(taskActualTimeSpent);
				}else{
					double taskActualTime = b_T_Ufp_TaskManager.getTaskActualTimeSpent()+taskActualTimeSpent;
					b_T_Ufp_TaskManager.setTaskActualTimeSpent(taskActualTime);
				}
				b_T_Ufp_TaskManager.setTaskSpeed(taskSpeed);
				b_T_Ufp_TaskManager.setRemark(remark+"<br />"+b_T_Ufp_TaskManager.getRemark());
			}
			i=b_T_Ufp_TaskManagerService.update(b_T_Ufp_TaskManager,tab,taskActualTimeSpent,taskActualSpent,remark);
		}else{
			i=b_T_Ufp_TaskManagerService.update(b_T_Ufp_TaskManager,tab,0.0,"",remark);
		}
		if(i >0 )
		return ajaxForwardSuccess(getText("msg.operation.success"), "TaskManageLists","", "forward", "projectmanagement/TaskManageLists?b_T_Ufp_TaskManager.accusedPeople="+SessionFactory.getSessionUserRealNam()+"&b_T_Ufp_TaskManager.taskSpeed=0");
		return ajaxForwardError(getText("msg.operation.failure"));
	}
	/**
	 * 查询
	 * @return
	 */

	public  String taskManageList() 
	{
		pager = new Pager<B_T_Ufp_TaskManager>(pageNum, numPerPage,orderField,orderDirection);
		if(b_T_Ufp_TaskManager != null){
		if(b_T_Ufp_TaskManager.getAccusedPeople()!=null && !"".equals(b_T_Ufp_TaskManager.getAccusedPeople())){
			try {
				if(!SessionFactory.getSessionUserRealNam().equals(b_T_Ufp_TaskManager.getAccusedPeople())){
					b_T_Ufp_TaskManager.setAccusedPeople(new String(b_T_Ufp_TaskManager.getAccusedPeople().getBytes("ISO8859_1"),"utf-8"));
				}
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(b_T_Ufp_TaskManager.getSentPeople()!=null ){
			try {
				if(!SessionFactory.getSessionUserRealNam().equals(b_T_Ufp_TaskManager.getSentPeople())){
				b_T_Ufp_TaskManager.setSentPeople(new String(b_T_Ufp_TaskManager.getSentPeople().getBytes("ISO8859_1"),"utf-8"));
				}
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		}
		if(b_T_Ufp_TaskManager.getTaskSpeed() != null && "1".equals(b_T_Ufp_TaskManager.getTaskSpeed())){
			if("create_time".equals(pager.getOrderField())){
				pager.setOrderField("Task_actual_end_time");
				pager.setOrderDirection("DESC");
			}
		}else{
			if("create_time".equals(pager.getOrderField())){
				pager.setOrderField("task_start_time");
				pager.setOrderDirection("ASC");
			}
		}
		pager= b_T_Ufp_TaskManagerService.selectList(b_T_Ufp_TaskManager,pager,flag); 
		return SUCCESS;
	}
	
	/**
	 * 删除
	 * @return
	 */
	public String taskManageDel()
	{
		int i=b_T_Ufp_TaskManagerService.delete(checkIds);
		if(i>0)
		return ajaxForwardSuccess(getText("msg.operation.success"), "TaskManageList","", "forward", "projectmanagement/TaskManageList?b_T_Ufp_TaskManager.taskSpeed=0");
		return ajaxForwardError(getText("msg.operation.failure"));
	
	}

	//跳转到新建页面
	public String taskManageDital() throws Exception
	{
		if(b_T_Ufp_TaskManager==null){
			b_T_Ufp_TaskManager=b_T_Ufp_TaskManagerService.selectOne("");
			//为页面设置以下值
			b_T_Ufp_TaskManager.setSentPeople(SessionFactory.getSessionUserRealNam());
			if(projectName!= null){
				b_T_Ufp_TaskManager.setProjectUuid(projectUuid);
				b_T_Flow_ProjectManagement = b_T_Flow_ProjectManagementService.selectOne(projectUuid);
				//String projectname =  new String(projectName.getBytes("ISO-8859-1"), "utf-8");
				b_T_Ufp_TaskManager.setRespectiveProject(b_T_Flow_ProjectManagement.getProjectName());
			}
			b_T_Ufp_TaskManager.setTaskid(UUID.randomUUID().toString());
			flag="add";
		}else{
			b_T_Ufp_TaskManager=b_T_Ufp_TaskManagerService.selectOne(b_T_Ufp_TaskManager.getTaskid());
			if(!"9".equals(flag)){
				b_T_Ufp_TaskManager.setSentPeople(SessionFactory.getSessionUserRealNam());
			}
			//查询项目下附件
			pagerFile = new Pager<C_T_Sys_File>(pageNum, numPerPage,orderField,orderDirection);
			pagerFile= c_T_Sys_FileService.selectLists(c_T_Sys_File,pagerFile,b_T_Ufp_TaskManager.getTaskid(),flag);
			flag = "edit";
		}
		return SUCCESS;
		
	}

}
